<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeUpdate_time"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['md:partner:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['md:partner:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['md:partner:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['md:partner:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile"/>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="社会编码" align="center" prop="idCard"/>
      <el-table-column label="诞生日期" align="center" prop="birthday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="是否启用" align="center" prop="enableFlag">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="sys_state" :value="scope.row.enableFlag"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="是否客户" align="center" prop="isCustomer">
        <template #default="scope">
          <dict-tag :options="sys_state" :value="scope.row.isCustomer"/>
        </template>
      </el-table-column>
      <el-table-column label="是否供应商" align="center" prop="isSupplier">
        <template #default="scope">
          <dict-tag :options="sys_state" :value="scope.row.isSupplier"/>
        </template>
      </el-table-column>
      <el-table-column label="是否公司" align="center" prop="isCompany">
        <template #default="scope">
          <dict-tag :options="sys_state" :value="scope.row.isCompany"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['md:partner:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['md:partner:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改合作伙伴对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="partnerRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                    v-for="dict in sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="社会编码" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入社会编码"/>
            </el-form-item>
            <el-form-item label="诞生日期" prop="birthday">
              <el-date-picker clearable
                              v-model="form.birthday"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择诞生日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机号"/>
            </el-form-item>
            <el-form-item label="上级" prop="parentId">
              <el-input v-model="form.parentId" placeholder="请输入上级"/>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="是否启用" prop="enableFlag">
              <el-switch
                  v-model="form.enableFlag"
                  active-value="0"
                  inactive-value="1"
              ></el-switch>
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="是否客户" prop="isCustomer">
              <el-switch
                  v-model="form.isCustomer"
                  active-value="0"
                  inactive-value="1"
              ></el-switch>
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="是否供应商" prop="isSupplier">
              <el-switch
                  v-model="form.isSupplier"
                  active-value="0"
                  inactive-value="1"
              ></el-switch>
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="是否公司" prop="isCompany">
              <el-switch
                  v-model="form.isCompany"
                  active-value="0"
                  inactive-value="1"
              ></el-switch>
            </el-form-item></el-col>

        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Partner">
import {listPartner, getPartner, delPartner, addPartner, updatePartner} from "@/api/md/partner";

const {proxy} = getCurrentInstance();
const {sys_user_sex, sys_state} = proxy.useDict('sys_user_sex', 'sys_state');
const route = useRoute();
const partnerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeUpdate_time = ref([]);

const data = reactive({
  form: {
    isSupplier: "1",
    isCustomer: "1",
    enableFlag: "0"
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mobile: null,
    isSupplier: null,
    isCustomer: null,
    updateTime: null,
    params: []
  },
  rules: {
    enableFlag: [{
      required: true,
      message: "是否不能为空",
      trigger: "change"
    }],
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "change"
    }],
    mobile: [{
      required: true,
      message: "手机号不能为空",
      trigger: "change"
    }]
  }
});

const {queryParams, form, rules} = toRefs(data);


/** 查询合作伙伴列表 */
function getList() {
  loading.value = true;
  if (null != daterangeUpdate_time && '' != daterangeUpdate_time) {
    queryParams.value.params["beginUpdate_time"] = daterangeUpdate_time.value[0];
    queryParams.value.params["endUpdate_time"] = daterangeUpdate_time.value[1];
  }
  listPartner(queryParams.value).then(response => {
    partnerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    name: null,
    sex: null,
    team_id: null,
    mobile: null,
    enableFlag: "0",
    email: null,
    idCard: null,
    birthday: null,
    isCustomer: null,
    isSupplier: null,
    isCompany: "1",
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  form.value.isSupplier = route.query.isSupplier;
  form.value.isCustomer = route.query.isCustomer;
  proxy.resetForm("partnerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.params = {};
  daterangeUpdate_time.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.isSupplier = route.query.isSupplier;
  queryParams.value.isCustomer = route.query.isCustomer;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加合作伙伴";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPartner(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改合作伙伴";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["partnerRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePartner(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPartner(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除合作伙伴编号为"' + _ids + '"的数据项？').then(function () {
    return delPartner(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('md/partner/export', {
    ...queryParams.value
  }, `partner_${new Date().getTime()}.xlsx`)
}
// debugger;
// queryParams.value = route.query;
queryParams.value.isSupplier = route.query.isSupplier;
queryParams.value.isCustomer = route.query.isCustomer;
// console.log(queryParams.value)
getList();
</script>
